#import "SetPasswordViewController.h"
#import "PopUIManager.h"
#import "JsonUtil.h"
#import "NetWorkManager.h"

#import "RequestServerAPI.h"

@interface SetPasswordViewController ()

@end

@implementation SetPasswordViewController
@synthesize authCodeInput, pwdInput, phoneNumber,okButton;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    
    okButton.layer.cornerRadius = 5;
    [okButton setBackgroundColor:DefaultNavigationColor];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    self.navigationItem.title = @"设置密码";
    
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@""
                                   style:UIBarButtonItemStylePlain
                                   target:nil
                                   action:nil];
    self.navigationItem.backBarButtonItem=backButton;
}
- (IBAction)backButton:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)okButton:(id)sender {
    NSString* authCode = authCodeInput.text;
    NSString* password = pwdInput.text;
    if (authCode.length == 0 || password.length < 6) {
        [[PopUIManager getInstanse] popAlertView:self title:@"提示" message:@"信息填写不完整" cancelButtonTitle:@"确定"];
        return;
    }
   // [RequestServerAPI apiResetPassword:self phoneNumber:phoneNumber authCode:authCode password:password];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:password, @"newpwd",authCode,@"authcode",phoneNumber,@"account",@"1",@"type", nil];
    
    NSString *dataString = [JsonUtil convertDictionaryToJson:dict];
    
    NSString *msg = @"USER_DO_RESET_PWD";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:self.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
         [popUIMgr removeWaitingUI];
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 1:{
                            ret_msg= @"Success";
                            break;
                        }case 10004:{
                            ret_msg= @"验证码失效";
                            break;
                        }case 10003:{
                            ret_msg= @"验证码不存在";
                            break;
                        }case 10005:{
                            ret_msg= @"数据库操作异常，请重试";
                            break;
                        }case 1001:{
                            ret_msg= @"用户不存在";
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"Success"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                       
                    }else{
                        [popUIMgr popAlertView:nil title:@"提示" message:@"密码修改成功" cancelButtonTitle:@"确定"];
                         [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
       
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
